package com.guyaning.deyson_core.core.app;

import android.app.Activity;
import android.os.Handler;

import com.blankj.utilcode.util.Utils;
import com.joanzapata.iconify.IconFontDescriptor;
import com.joanzapata.iconify.Iconify;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.HashMap;

import okhttp3.Interceptor;

/**
 * Created by Administrator on 2018/3/21.
 */

public final  class Configurator {

    private static final HashMap<Object,Object> DEYSON_CONFIGS = new HashMap<>();

    private static final Handler HANDLER = new Handler();

    private static final ArrayList<IconFontDescriptor> ICONS = new ArrayList<>();

    private static final ArrayList<Interceptor> INTERCEPTORS = new ArrayList<>();

    private Configurator() {
        DEYSON_CONFIGS.put(Config_Type.CONFIG_READY, false);
        DEYSON_CONFIGS.put(Config_Type.HANDLER, HANDLER);
    }

    //最佳单例模式，静态内部类
    public static Configurator getInstance(){
        return Holder.INSTANCE ;
    }

    private  static  class Holder{
        private static final  Configurator INSTANCE = new Configurator();
    }

    final HashMap<Object,Object> getDEYSON_CONFIGS(){
        return DEYSON_CONFIGS;
    }

    public final void configure(){
        Logger.addLogAdapter(new AndroidLogAdapter());

        DEYSON_CONFIGS.put(Config_Type.CONFIG_READY,true);

        Utils.init(Deyson.getApplicationContext());
    }

    public final Configurator withApiHost(String host){
        DEYSON_CONFIGS.put(Config_Type.API_HOST,host);
        return  this;
    }

    private void  CheckConfiguration(){
        final  boolean isReady = (boolean) DEYSON_CONFIGS.get(Config_Type.CONFIG_READY);

        if(!isReady){
            throw new RuntimeException("Configuration is not ready,please call configurator");
        }
    }

    @SuppressWarnings("unchecked")
    final<T> T getConfiguration(Object key){
       CheckConfiguration();
        final Object value = DEYSON_CONFIGS.get(key);
        if (value == null) {
            throw new NullPointerException(key.toString() + " IS NULL");
        }
        return (T) DEYSON_CONFIGS.get(key);
    }

    private void initIcons() {
        if (ICONS.size() > 0) {
            final Iconify.IconifyInitializer initializer = Iconify.with(ICONS.get(0));
            for (int i = 1; i < ICONS.size(); i++) {
                initializer.with(ICONS.get(i));
            }
        }
    }

    public final Configurator withIcon(IconFontDescriptor descriptor) {
        ICONS.add(descriptor);
        return this;
    }

    public final Configurator withInterceptor(Interceptor interceptor) {
        INTERCEPTORS.add(interceptor);
        DEYSON_CONFIGS.put(Config_Type.INTERCEPTOR, INTERCEPTORS);
        return this;
    }

    public final Configurator withInterceptors(ArrayList<Interceptor> interceptors) {
        INTERCEPTORS.addAll(interceptors);
        DEYSON_CONFIGS.put(Config_Type.INTERCEPTOR, INTERCEPTORS);
        return this;
    }

    public final Configurator withWeChatAppId(String appId) {
        DEYSON_CONFIGS.put(Config_Type.WE_CHAT_APP_ID, appId);
        return this;
    }

    public final Configurator withWeChatAPPSecret(String appSecret) {
        DEYSON_CONFIGS.put(Config_Type.WE_CHAT_APP_SECRET, appSecret);
        return this;
    }

    public final Configurator withActivity(Activity activity) {
        DEYSON_CONFIGS.put(Config_Type.ACTIVITY, activity);
        return this;
    }

}
